package com.newshub.business.controller;

import com.newshub.common.Result;
import com.newshub.business.dto.PageResult;
import com.newshub.business.entity.Category;
import com.newshub.business.entity.News;
import com.newshub.business.entity.Source;
import com.newshub.business.service.NewsService;
import com.newshub.business.service.CategoryService;
import com.newshub.business.service.SourceService;
import com.newshub.logging.annotation.BusinessLog;
import com.newshub.logging.annotation.LogLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import com.newshub.business.dto.NewsQueryDTO;

/**
 * 新闻控制器
 */
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*") // 允许跨域访问
public class NewsController {
    
    @Autowired
    private NewsService newsService;
    
    @Autowired
    private CategoryService categoryService;
    
    @Autowired
    private SourceService sourceService;
    
    /**
     * 获取分类列表
     * GET /api/categories
     */
    @GetMapping("/categories")
    @BusinessLog(level = LogLevel.INFO, description = "获取分类列表")
    public Result<List<Category>> getCategories() {
        List<Category> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }
    
    /**
     * 获取所有新闻源列表
     * GET /api/sources
     */
    @GetMapping("/sources")
    @BusinessLog(level = LogLevel.INFO, description = "获取新闻源列表")
    public Result<List<Source>> getSources() {
        List<Source> sources = sourceService.getAllSources();
        return Result.success(sources);
    }
    
    /**
     * 获取新闻源卡片数据
     * GET /api/sources/cards
     */
    @GetMapping("/sources/cards")
    @BusinessLog(level = LogLevel.INFO, description = "获取新闻源卡片数据")
    public Result<List<Map<String, Object>>> getSourceCards() {
        List<Map<String, Object>> sourceCards = newsService.getSourceCards();
        return Result.success(sourceCards);
    }
    
    /**
     * 获取指定信源的新闻列表
     * GET /api/sources/{id}/news
     */
    @GetMapping("/sources/{id}/news")
    @BusinessLog(level = LogLevel.INFO, description = "获取指定信源的新闻列表")
    public Result<List<News>> getNewsBySource(
            @PathVariable Integer id,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(defaultValue = "10") Integer limit) {
        
        List<News> newsList = newsService.getNewsBySourceId(id, categoryId, limit);
        return Result.success(newsList);
    }
    
    /**
     * 获取新闻详情
     * GET /api/news/{id}
     */
    @GetMapping("/news/{id}")
    @BusinessLog(level = LogLevel.INFO, description = "获取新闻详情")
    public Result<News> getNewsDetail(@PathVariable Integer id) {
        News news = newsService.getNewsById(id);
        if (news == null) {
            return Result.error(404, "新闻不存在");
        }
        return Result.success(news);
    }
    
    /**
     * 获取首页新闻列表
     * GET /api/news
     */
    @GetMapping("/news")
    @BusinessLog(level = LogLevel.INFO, description = "获取首页新闻列表")
    public Result<List<News>> getHomeNews(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer sourceId,
            @RequestParam(defaultValue = "20") Integer limit) {
        
        List<News> newsList = newsService.getHomeNews(categoryId, sourceId, limit);
        return Result.success(newsList);
    }
    
    /**
     * 搜索新闻
     * GET /api/news/search
     */
    @GetMapping("/news/search")
    @BusinessLog(level = LogLevel.INFO, description = "搜索新闻")
    public Result<PageResult<News>> searchNews(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        // 如果没有关键词，返回所有新闻的分页查询
        if (keyword == null || keyword.trim().isEmpty()) {
            // 使用现有的分页查询方法
            NewsQueryDTO queryDTO = new NewsQueryDTO();
            queryDTO.setPageNum(pageNum);
            queryDTO.setPageSize(pageSize);
            PageResult<News> result = newsService.getNewsPage(queryDTO);
            return Result.success(result);
        }
        
        // 有关键词时，使用搜索功能
        PageResult<News> result = newsService.searchNewsPage(keyword.trim(), pageNum, pageSize);
        return Result.success(result);
    }
}